package net.maergaiyun.controller;


import com.google.code.kaptcha.Producer;
import lombok.extern.slf4j.Slf4j;
import net.maergaiyun.controller.request.SendCodeRequest;
import net.maergaiyun.enums.BizCodeEnum;
import net.maergaiyun.enums.SendCodeEnum;
import net.maergaiyun.service.NotifyService;
import net.maergaiyun.util.CommonUtil;
import net.maergaiyun.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/api/notify/v1")
@Slf4j
public class NotifyController {

    @Autowired
    private NotifyService notifyService;

    @Autowired
    private Producer captchaProducer;

    @Autowired
    private StringRedisTemplate redisTemplate;

    private static final long CAPTCHA_CODE_EXPIRED = 1000 * 60 * 10;

    @GetMapping("captcha")
    public void getCaptcha(HttpServletRequest request, HttpServletResponse response) {
        String captchaText = captchaProducer.createText();
        log.info("验证码内容: {}", captchaText);
        // 存储redis
        redisTemplate.opsForValue().set(getCaptchaKey(request), captchaText, CAPTCHA_CODE_EXPIRED, TimeUnit.MILLISECONDS);

        BufferedImage bufferImage = captchaProducer.createImage(captchaText);

        try (ServletOutputStream outputStream = response.getOutputStream()) {
            ImageIO.write(bufferImage, "jpg", outputStream);
            outputStream.flush();

        } catch (IOException e) {
            log.info("获取流出错: {}", e.getMessage());
        }
    }

    private String getCaptchaKey(HttpServletRequest request) {
        String ip = CommonUtil.getIpAddr(request);
        String userAgent = request.getHeader("User-Agent");

        String key = "account-service:captcha:" + CommonUtil.MD5(ip + userAgent);
        log.info("验证码key: {}", key);
        return key;
    }

    //    @RequestMapping("send_code")
    //    public JsonData testSend() {
    //        notifyService.testSend();
    //        return JsonData.buildSuccess();
    //    }

    @RequestMapping("send_code")
    public JsonData send(@RequestBody SendCodeRequest sendCodeRequest, HttpServletRequest request) {
        String key = getCaptchaKey(request);
        String cacheCaptcha = redisTemplate.opsForValue().get(key);
        String captcha = sendCodeRequest.getCaptcha();
        if (captcha != null && cacheCaptcha != null && captcha.equalsIgnoreCase(cacheCaptcha)) {
            redisTemplate.delete(key);
            JsonData jsonData = notifyService.sendCode(SendCodeEnum.USER_REGISTER, sendCodeRequest.getTo());
            return jsonData;
        } else {
            return JsonData.buildResult(BizCodeEnum.CODE_CAPTCHA_ERROR);
        }
    }


}
